<?php
/**
 * Created by PhpStorm.
 * User: meng
 * Date: 2021/7/5
 * Time: 10:42 下午
 */

namespace app\lib;


use app\lib\exception\ParamsException;
use think\facade\Request;
use think\Validate;

class BaseValidator extends Validate
{
    public function goCheck ($data = [], $scene = null): bool {
        // 需要验证的数据
        $params = empty($data) ? Request::param() : $data;
        // 是否有验证场景
        $valid = empty($scene) ? $this->check($params) : $this->scene($scene)->check($params);
        if (!$valid) {
            throw new ParamsException(['msg' => $this->error]);
        }
        return true;
    }

    protected function isMobileNumber ($value): bool {
        $rule = '^1(2|3|4|5|6|7|8|9)[0-9]\d{8}$^';
        $result = preg_match($rule, $value);
        if ($result) {
            return true;
        } else {
            return false;
        }
    }

    protected function isPostInt ($value): bool {
        if (is_numeric($value) && is_int($value + 0) && ($value + 0) > 0) {
            return true;
        }
        return false;
    }

    protected function passwod ($value): bool {
        if(!preg_match("/^(?![0-9]+$)(?![a-zA-Z]+$)(?!([^(0-9a-zA-Z)]|[\(\)])+$)([^(0-9a-zA-Z)]|[\(\)]|[a-zA-Z]|[0-9]){6,}$/",$value)) {
            return false;
        }
        return true;
    }
}
